// pages/personal/personal.js
import axios from '../../utils/axios'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moveDistance: 0,
    userId: 0,
    personalPlayList: [],
    weekList: [], //7天内听歌记录,
    createdList: [], //创建的歌单
    collectedList: [] //收藏的歌单
  },


  // 手指移动事件
  coverMove(event) {
    if (event.type == 'touchstart') {
      this.start = event.changedTouches[0].clientY
    }
    if (event.type == 'touchmove') {
      this.move = event.changedTouches[0].clientY
      let moveDistance = this.move - this.start
      if (moveDistance > 80 || moveDistance < 0) return
      this.setData({
        moveDistance,
      })
    }
    if (event.type == 'touchend') {
      this.setData({
        moveDistance: 0
      })
    }
  },

  // 去登陆页面
  tologin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },

  // 手指抬起事件
  // coverUp:(event)=>{
  //   console.log(event);
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const userId = wx.getStorageSync('userId')
    if (!userId) return
    this.setData({
      userId
    })
    axios('/user/playlist', {
      uid: userId
    }).then((res) => {
      this.setData({
        personalPlayList: res.playlist,
        createdList:res.playlist.splice(1).filter(item => {
          return !item.subscribed
        }),
        collectedList:res.playlist.splice(1).filter(item => {
          return item.subscribed
        }),
      })
    })

    axios('/user/record', {
      uid: userId,
      type: 1
    }).then((res) => {
      this.setData({
        weekList: res.weekData.map(item => {
          return {
            id: item.song.al.id,
            imgUrl: item.song.al.picUrl,
            songName: item.song.name,
          }
        })
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})